cmake_minimum_required(VERSION 3.14)

set(MONOPP_MAIN_PROJECT ON)
if(DEFINED PROJECT_SOURCE_DIR)
    set(MONOPP_MAIN_PROJECT OFF)
endif()

project(monopp)
include(CTest)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake/utility")

option(BUILD_MONOPP_SHARED "Build as a shared library." ON)
option(BUILD_MONOPP_MONORT "Build the monort utility library" ON)
option(BUILD_MONOPP_TESTS "Build the tests" ${MONOPP_MAIN_PROJECT})

option(BUILD_MONOPP_WITH_CODE_STYLE_CHECKS "Build with code style checks." OFF)

if(BUILD_MONOPP_TESTS)
	if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
		set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
	endif()
    if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
        set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
    endif()
	if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
		set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
	endif()

    # Second, for multi-config builds (e.g. msvc)
    foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
        string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
        set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
        set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
        set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} )
    endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

    message(STATUS "Enabled ${PROJECT_NAME} tests.")

	set(BUILD_MONOPP_MONORT ON)
endif()

if(BUILD_MONOPP_SHARED)
	set(BUILD_SHARED_LIBS ON)
endif()

if(BUILD_MONOPP_WITH_CODE_STYLE_CHECKS)
	set(BUILD_WITH_CODE_STYLE_CHECKS ON)
endif()

add_subdirectory(monopp)

if(BUILD_MONOPP_MONORT)	
	add_subdirectory(monort)
endif()

if(BUILD_MONOPP_TESTS)
    add_subdirectory(3rdparty)
    add_subdirectory(tests)

    set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)
    include(InstallRequiredSystemLibraries)

    if(MINGW)
        get_filename_component( Mingw_Path ${CMAKE_CXX_COMPILER} PATH )
        file(GLOB MINGW_SYSTEM_RUNTIME_LIBS "${Mingw_Path}/*.dll")
        set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} ${MINGW_SYSTEM_RUNTIME_LIBS})
    endif()
    message(STATUS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS})


    if(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
    add_custom_target(copy_compiler_dependencies ALL
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
    endif()

    if(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
        install( PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
    endif()

endif()

